package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Activity;
import ysq.xyg.model.info.ActivityInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.ActivityService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * 文章管理
 */
@Controller
@RequestMapping("/activity")
public class ActivityController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(ActivityController.class);

    @Resource
    private ActivityService activityService;

    private Activity activity;

    @RequestMapping("/listPage")
    public ModelAndView List(){
        return new ModelAndView("/activityList");
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(Activity activity,Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<Activity> pageInfo = activityService.pageInfo(activity, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            jsonObject.put("data",pageInfo.getList());
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id,String type){
        ModelAndView view = new ModelAndView("/activityUpdateOrSave");
        activity = activityService.selectByPrimaryKey(id);
        view.addObject("activity", activity);
        view.addObject("type", type);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(ActivityInfo activityInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(activityInfo.getId())) {
            activity = new Activity();
            activity.setId(BaseUtil.uId());
            activity.setName(activityInfo.getName());
            activity.setPicture(activityInfo.getPicture());
            activity.setOrganizers(activityInfo.getOrganizers());
            activity.setAddress(activityInfo.getAddress());
            activity.setPhone(activityInfo.getPhone());
            activity.setDescription(activityInfo.getDescription());
            if(StringUtil.isNotEmpty(activityInfo.getStartDate())){
                activity.setStartTime(DateUtil.strToDate(DateUtil.DATE_TIME_PATTERN,activityInfo.getStartDate()));
            }
            if(StringUtil.isNotEmpty(activityInfo.getEndDate())){
                activity.setEndTime(DateUtil.strToDate(DateUtil.DATE_TIME_PATTERN,activityInfo.getEndDate()));
            }
            activity.setMoney(activityInfo.getMoney());
            activity.setCreateTime(new Date());

            int id = activityService.insert(activity);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            activity = activityService.selectByPrimaryKey(activityInfo.getId());
            activity.setName(activityInfo.getName());
            activity.setPicture(activityInfo.getPicture());
            activity.setOrganizers(activityInfo.getOrganizers());
            activity.setAddress(activityInfo.getAddress());
            activity.setPhone(activityInfo.getPhone());
            activity.setDescription(activityInfo.getDescription());
            if(StringUtil.isNotEmpty(activityInfo.getStartDate())){
                activity.setStartTime(DateUtil.strToDate(DateUtil.DATE_TIME_PATTERN,activityInfo.getStartDate()));
            }
            if(StringUtil.isNotEmpty(activityInfo.getEndDate())){
                activity.setEndTime(DateUtil.strToDate(DateUtil.DATE_TIME_PATTERN,activityInfo.getEndDate()));
            }
            activity.setMoney(activityInfo.getMoney());
            int id = activityService.updateByPrimaryKey(activity);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
           int ids= activityService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
